package com.sie.demo.controller;


import com.sie.demo.entity.Menu;
import com.sie.demo.entity.RetResponse;
import com.sie.demo.entity.RetResult;
import com.sie.demo.entity.Users;

import com.sie.demo.entity.vo.PageUserVo;
import com.sie.demo.entity.vo.QueryUserVo;
import com.sie.demo.service.impl.UserRoleServiceImpl;
import com.sie.demo.service.impl.UserServiceImpl;
import com.sie.demo.util.TokenUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;


@Controller
@RestController
@RequestMapping("user")
@Slf4j
public class UserController {
    @Autowired
    UserServiceImpl userService;
    @Autowired
    UserRoleServiceImpl userRoleService;

    /**
     * @param username
     * @param password
     * @param code
     * @param session
     * @return RetResult<Users>
     */
    @RequestMapping("login")
    RetResult<Users> login(String username, String password, String code, HttpSession session) {
        //        校验验证码
        LoginController loginController = new LoginController();
        if (!loginController.checkVerify(code, session)) {
            return RetResponse.makeErrRsp("验证码错误");
        }
        Users users = userService.login(username, password, code, session);
        if (users == null || users.getStatus() == 0) {
            return RetResponse.makeErrRsp("账号或密码错误");
        } else {
            String token = TokenUtil.sign(new Users(username, password));

            return RetResponse.makeOKRsp("登录成功", users, token);
        }
    }

    @RequestMapping("getMenuList")
    RetResult<List> getMenuList(Integer userId) {
        List<Menu> list = userService.findAllMenuByUserId(userId);
        return RetResponse.makeOKRsp(list);
    }

    /**
     * @param users
     * @return
     */
    @PostMapping("add")
    RetResult<Users> addUser(Users users) {
//        获取当前系统时间
        Date date = new Date();
//        添加到Oracle数据库中的时间格式为：【 年/月/日  时:分:秒 】
        java.sql.Timestamp createTime = new java.sql.Timestamp(date.getTime());
        users.setCreateTime(createTime);
        users.setUpdateTime(createTime);
        int i = userService.addUsers(users);
        if (i == 1) {
            return RetResponse.makeOKRsp("插入成功", users, "");
        } else {
            return RetResponse.makeRsp(500, "服务器异常");
        }
    }

    /**
     * 编辑用户
     *
     * @param users
     * @return
     */
    @PostMapping("edit")
    public RetResult<Users> editUser(Users users) {
        //        获取当前系统时间
        Date date = new Date();
//        添加到Oracle数据库中的时间格式为：【 年/月/日  时:分:秒 】
        java.sql.Timestamp updateTime = new java.sql.Timestamp(date.getTime());
        users.setUpdateTime(updateTime);
        int i = userService.editUser(users);
        if (i == 1) {
            return RetResponse.makeOKRsp("修改成功", users, "");
        } else {
            return RetResponse.makeRsp(500, "服务器异常");
        }
    }

    /**
     * @param currentPage
     * @param pageSize
     * @param queryUserVo 模糊查询的条件封装
     * @return
     */
    @GetMapping("selectAll")
    public RetResult<PageUserVo> selectAll(long currentPage, long pageSize, QueryUserVo queryUserVo) {
        PageUserVo pageUserVo = userService.selectAll(currentPage, pageSize, queryUserVo);
        return RetResponse.makeOKRsp(pageUserVo);
    }

    @DeleteMapping("delete")
    public RetResult<Users> deleteUserByIds(String[] ids) {
        Integer isDelete = userService.deleteUserByIds(ids);
        if (isDelete != 0) {
            return RetResponse.makeRsp(200, "删除成功");
        } else {
            return RetResponse.makeRsp(500, "删除失败");
        }
    }

    @GetMapping("getOne")
    public RetResult<Users> getOne(String userName) {
        Users users = userService.getOne(userName);
        return RetResponse.makeOKRsp(users);

    }

    //    根据id获得角色数组
    @GetMapping("getRolesByUserId")
    public RetResult<List> getRolesByUserId(Integer userId) {
        List<Integer> list = userRoleService.selectRoleByUserId(userId);
        return RetResponse.makeOKRsp(list);

    }
//    给用户设置角色
    @PutMapping("grantPower")
    public RetResult<List> grantPower(Integer userId,String[] roleSelected) {
        if (roleSelected.length!=0){
            if (userId!=null){
                userRoleService.deleteByUserId(userId);
            }
            userRoleService.addRoleByUserID(userId,roleSelected);
            return RetResponse.makeRsp(200, "设置成功");
        }else {
            return RetResponse.makeRsp(200, "请选择至少一个角色");
        }

    }

//   在SQL映射文件中使用if、choose、where、set实现动态SQL的映射
}
